package com.kezesong.leasing.controller.admin;

import com.kezesong.leasing.common.ResponseResult;
import com.kezesong.leasing.controller.BaseController;
import com.kezesong.leasing.controller.request.admin.AddCategoryReqBody;
import com.kezesong.leasing.controller.request.admin.ModifyCategoryReqBody;
import com.kezesong.leasing.entity.GoodsCategoryEntity;
import com.kezesong.leasing.service.GoodsCategoryService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/admin")
public class CategoryManagerController extends BaseController {

    @Autowired
    private GoodsCategoryService goodsCategoryService;

    @PostMapping("/getCategoryList.do")
    @ApiOperation(value = "getCategoryList", notes = "管理员获取全部商品类别列表")
    public ResponseResult<List<GoodsCategoryEntity>> getCategoryList(){
        List<GoodsCategoryEntity> goodsCategoryList = goodsCategoryService.getGoodsCategoryList();
        if (null == goodsCategoryList || goodsCategoryList.size() == 0){
            return buildNoDataResponse();
        }else {
            ResponseResult<List<GoodsCategoryEntity>> listResponseResult = buildSucessResponse(goodsCategoryList);
            return listResponseResult;
        }
    }


    @PostMapping("/addCategoryList.do")
    @ApiOperation(value = "addCategory", notes = "添加类别列表")
    public ResponseResult<String> addCategory(@RequestBody AddCategoryReqBody reqBody){
        GoodsCategoryEntity goodsCategoryEntity = new GoodsCategoryEntity();
        goodsCategoryEntity.setCategoryName(reqBody.getCategoryName());
        goodsCategoryEntity.setCategoryFlag(reqBody.getCategoryFlag());
        goodsCategoryEntity.setShowOrder(reqBody.getShowOrder());
        int rows = goodsCategoryService.addGoodsCategory(goodsCategoryEntity);
        ResponseResult<String> responseResult = new ResponseResult<>();
        if (rows<=0){
            responseResult  = buildFailedResponse();
        }
        return responseResult;
    }

    @GetMapping("/deleteCategoryList/{categoryId}")
    @ApiOperation(value = "deleteCategory", notes = "删除类别列表")
    public ResponseResult<String> deleteCategory(@PathVariable("categoryId") int categoryId){
        int rows = goodsCategoryService.deleteGoodsCategory(categoryId);
        ResponseResult<String> responseResult = new ResponseResult<>();
        if (rows<=0){
            responseResult  = buildFailedResponse();
        }
        return responseResult;
    }


    @PostMapping("/modifyCategory.do")
    @ApiOperation(value = "modifyCategory", notes = "修改类别列表")
    public ResponseResult<String> modifyCategory(@RequestBody ModifyCategoryReqBody reqBody){
        GoodsCategoryEntity goodsCategoryEntity = new GoodsCategoryEntity();
        BeanUtils.copyProperties(reqBody, goodsCategoryEntity);
        int rows = goodsCategoryService.modifyGoodsCategory(goodsCategoryEntity);
        ResponseResult<String> responseResult = new ResponseResult<>();
        if (rows<=0){
            responseResult  = buildFailedResponse();
        }
        return responseResult;
    }


}
